

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/Mine.jpg">
  <link rel="icon" href="/img/Mine.jpg">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Chiam">
  <meta name="keywords" content="算法，安全">
  
    <meta name="description" content="『C++&amp;C 语言』c++如何获取系统当前时间（超级详细）12345678910void gettime() &amp;#123;     time_t rawtime;     struct tm *ptminfo;     time(&amp;rawtime);     ptminfo &#x3D; localtime(&amp;rawtime);     printf(&quot;current: %">
<meta property="og:type" content="article">
<meta property="og:title" content="『C++&amp;C语言』c++如何获取系统当前时间（超级详细）">
<meta property="og:url" content="http://example.com/2023/12/06/%E3%80%8EC++&C%E8%AF%AD%E8%A8%80%E3%80%8Fc++%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96%E7%B3%BB%E7%BB%9F%E5%BD%93%E5%89%8D%E6%97%B6%E9%97%B4%EF%BC%88%E8%B6%85%E7%BA%A7%E8%AF%A6%E7%BB%86%EF%BC%89/index.html">
<meta property="og:site_name" content="Chiam 的个人主页">
<meta property="og:description" content="『C++&amp;C 语言』c++如何获取系统当前时间（超级详细）12345678910void gettime() &amp;#123;     time_t rawtime;     struct tm *ptminfo;     time(&amp;rawtime);     ptminfo &#x3D; localtime(&amp;rawtime);     printf(&quot;current: %">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-12-05T16:11:43.616Z">
<meta property="article:modified_time" content="2023-12-05T16:17:38.246Z">
<meta property="article:author" content="Chiam">
<meta property="article:tag" content="算法，安全">
<meta name="twitter:card" content="summary_large_image">
  
  
  
  <title>『C++&amp;C语言』c++如何获取系统当前时间（超级详细） - Chiam 的个人主页</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  



  
<link rel="stylesheet" href="/css/custom.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.5-a","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":"❡"},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":2},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":{"measurement_id":null},"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml","include_content_in_search":true};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Chiam&#39;s Blogs</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                
                <span>关于</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/">
                
                <span>友链</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="『C++&amp;C语言』c++如何获取系统当前时间（超级详细）"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-12-06 00:11" pubdate>
          2023年12月6日 凌晨
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          3.5k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          30 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <h1 id="seo-header">『C++&amp;C语言』c++如何获取系统当前时间（超级详细）</h1>
            
            
              <div class="markdown-body">
                
                <h1 id="『C-C-语言』c-如何获取系统当前时间（超级详细）"><a href="#『C-C-语言』c-如何获取系统当前时间（超级详细）" class="headerlink" title="『C++&amp;C 语言』c++如何获取系统当前时间（超级详细）"></a>『C++&amp;C 语言』c++如何获取系统当前时间（超级详细）</h1><figure class="highlight xl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs xl">void gettime()<br> &#123;<br>     time_t rawtime;<br>     struct tm *ptminfo;<br>     <span class="hljs-built_in">time</span>(&amp;rawtime);<br>     ptminfo = localtime(&amp;rawtime);<br>     printf(<span class="hljs-string">&quot;current: %02d-%02d-%02d %02d:%02d:%02d\n&quot;</span>,<br>     <span class="hljs-function"><span class="hljs-title">ptminfo</span>-&gt;</span><span class="hljs-function"><span class="hljs-title">tm_year</span> + 1900, ptminfo-&gt;</span><span class="hljs-function"><span class="hljs-title">tm_mon</span> + 1, ptminfo-&gt;</span>tm_mday,<br>     <span class="hljs-function"><span class="hljs-title">ptminfo</span>-&gt;</span><span class="hljs-function"><span class="hljs-title">tm_hour</span>, ptminfo-&gt;</span><span class="hljs-function"><span class="hljs-title">tm_min</span>, ptminfo-&gt;</span>tm_sec);<br> &#125;<br></code></pre></td></tr></table></figure>

<p>比较常用，保存一下。<br>C++中与系统时间相关的函数定义在<ctime>头文件中。</p>
<h5 id="详细介绍"><a href="#详细介绍" class="headerlink" title="详细介绍"></a>详细介绍</h5><p><strong>函数定义如下：</strong></p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-function"><span class="hljs-type">time_t</span> <span class="hljs-title">time</span> <span class="hljs-params">(<span class="hljs-type">time_t</span>* timer)</span></span>;<br></code></pre></td></tr></table></figure>

<p>获取系统当前日历时间 UTC 1970-01-01 00：00：00 开始的 unix 时间戳</p>
<p><strong>Coordinated Universal Time（UTC）</strong>：<br>协调世界时，又称为世界标准时间，也就是大家所熟知的格林威治标准时间（Greenwich Mean Time，GMT）。比如，中国内地的时间与 UTC 的时差为+8，也就是 UTC+8。美国是 UTC-5。</p>
<p><strong>参数：</strong><br>timer 存取结果的时间指针变量，类型为 time_t，指针变量可以为 null。<br>如果 timer 指针非 null，则 time()函数返回值变量与 timer 指针一样，都指向同一个内存地址；<br>否则如果 timer 指针为 null，则 time()函数返回一个 time_t 变量时间。<br>返回值，如果成功，获取当前系统日历时间，否则返回 -1。</p>
<p><strong>二、结构体 struct tm 中的属性</strong></p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-keyword">struct</span> <span class="hljs-title class_">tm</span><br>&#123;<br>        <span class="hljs-type">int</span> tm_sec;     <span class="hljs-comment">/* seconds after the minute - [0,59] */</span><br>        <span class="hljs-type">int</span> tm_min;     <span class="hljs-comment">/* minutes after the hour - [0,59] */</span><br>        <span class="hljs-type">int</span> tm_hour;    <span class="hljs-comment">/* hours since midnight - [0,23] */</span><br>        <span class="hljs-type">int</span> tm_mday;    <span class="hljs-comment">/* day of the month - [1,31] */</span><br>        <span class="hljs-type">int</span> tm_mon;     <span class="hljs-comment">/* months since January - [0,11] */</span><br>        <span class="hljs-type">int</span> tm_year;    <span class="hljs-comment">/* years since 1900 */</span><br>        <span class="hljs-type">int</span> tm_wday;    <span class="hljs-comment">/* days since Sunday - [0,6] */</span><br>        <span class="hljs-type">int</span> tm_yday;    <span class="hljs-comment">/* days since January 1 - [0,365] */</span><br>        <span class="hljs-type">int</span> tm_isdst;   <span class="hljs-comment">/* daylight savings time flag */</span><br>&#125;;<br></code></pre></td></tr></table></figure>

<p>这结构体，编译器内置。</p>
<table>
<thead>
<tr>
<th>变量</th>
<th>类型</th>
<th>说明</th>
<th>范围</th>
</tr>
</thead>
<tbody><tr>
<td>tm_sec</td>
<td>int</td>
<td>每分钟的秒数</td>
<td>[0 - 61]</td>
</tr>
<tr>
<td>tm_min</td>
<td>int</td>
<td>每小时后面的分钟数</td>
<td>[0 - 59]</td>
</tr>
<tr>
<td>tm_hour</td>
<td>int</td>
<td>凌晨开始的小时数</td>
<td>[0 - 23]</td>
</tr>
<tr>
<td>tm_mday</td>
<td>int</td>
<td>从每月份开始算的天数</td>
<td>[1 - 31]</td>
</tr>
<tr>
<td>tm_mon</td>
<td>int</td>
<td>从一月份开始的月份数</td>
<td>[0 - 11]</td>
</tr>
<tr>
<td>tm_year</td>
<td>int</td>
<td>从 1900 年开始的年数</td>
<td>None</td>
</tr>
<tr>
<td>tm_wday</td>
<td>int</td>
<td>从每周天开始算的天数</td>
<td>[0 - 6]</td>
</tr>
<tr>
<td>tm_yday</td>
<td>int</td>
<td>一年的第几天，从零开始</td>
<td>[0 - 365]</td>
</tr>
<tr>
<td>tm_isdst</td>
<td>int</td>
<td>夏令时</td>
<td>None</td>
</tr>
</tbody></table>
<p><strong>这里有几个地方要注意:</strong></p>
<ol>
<li>tm_sec 在 C89 的范围是[0-61]，在 C99 更正为[0-60]。通常范围是[0-59]，只是某些系统会出现 60 秒的跳跃。</li>
<li>tm_mon 是从零开始的，所以一月份为 0，十二月份为 11,计算月份的时候需要加 1。</li>
<li>tm_year 计算年份的时候需要加上 1900</li>
</ol>
<p>也可以使用本地时间函数<code>p=localtime（time_t* nowtime）</code>将 nowtime 变量中的日历时间转化为本地时间</p>
<p><strong>使用方法：</strong><br><strong>sec1：上面的方法</strong></p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;ctime&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;cstdio&gt;</span></span><br><span class="hljs-meta">#using namespace std;</span><br><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>&#123;<br>	<span class="hljs-type">time_t</span> nowtime;<br>	<span class="hljs-comment">//首先创建一个time_t 类型的变量nowtime</span><br>	<span class="hljs-keyword">struct</span> <span class="hljs-title class_">tm</span>* p;<br>	<span class="hljs-comment">//然后创建一个新时间结构体指针 p</span><br>	<span class="hljs-built_in">time</span>(&amp;nowtime);<br>	<span class="hljs-comment">//使用该函数就可得到当前系统时间，使用该函数需要将传入time_t类型变量nowtime的地址值。</span><br>	p = <span class="hljs-built_in">localtime</span>(&amp;nowtime);<br>	<span class="hljs-comment">//由于此时变量nowtime中的系统时间值为日历时间，我们需要调用本地时间函数p=localtime（time_t* nowtime）将nowtime变量中的日历时间转化为本地时间，存入到指针为p的时间结构体中。不改的话，可以参照注意事项手动改。</span><br>	<span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%02d:%02d:%02d\n&quot;</span>,p-&gt;tm_hour,p-&gt;tm_min,p-&gt;tm_sec);<br>	<span class="hljs-comment">//控制格式输出</span><br>	<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><strong>sec2：借助 <code>strftime()</code>函数</strong></p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-function"><span class="hljs-type">size_t</span> <span class="hljs-title">strftime</span><span class="hljs-params">(</span></span><br><span class="hljs-params"><span class="hljs-function"><span class="hljs-type">char</span> *strDest, <span class="hljs-comment">//目标字符串</span></span></span><br><span class="hljs-params"><span class="hljs-function"><span class="hljs-type">size_t</span> maxsize,<span class="hljs-comment">//最多传出字符数量</span></span></span><br><span class="hljs-params"><span class="hljs-function"><span class="hljs-type">const</span> <span class="hljs-type">char</span> *format,<span class="hljs-comment">//格式化方式</span></span></span><br><span class="hljs-params"><span class="hljs-function"><span class="hljs-type">const</span>  <span class="hljs-keyword">struct</span> tm *timeptr <span class="hljs-comment">//tm指针，上文讲的那个</span></span></span><br><span class="hljs-params"><span class="hljs-function">)</span></span>;<br></code></pre></td></tr></table></figure>

<p><strong>我们单独拿出 <code>*format</code>来</strong></p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs cpp">%a 星期几的简写<br>%A 星期几的全称<br>%b 月份的简写<br>%B 月份的全称<br>%c 标准的日期的时间串<br>%C 年份的前两位数字<br>%d 十进制表示的每月的第几天<br>%D 月/天/年<br>%e 在两字符域中，十进制表示的每月的第几天<br>%F 年-月-日<br>%g 年份的后两位数字，使用基于周的年<br>%G 年份，使用基于周的年<br>%h 简写的月份名<br>%H <span class="hljs-number">24</span>小时制的小时<br>%I <span class="hljs-number">12</span>小时制的小时<br>%j 十进制表示的每年的第几天<br>%m 十进制表示的月份<br>%M 十时制表示的分钟数<br>%n 新行符<br>%p 本地的AM或PM的等价显示<br>%r <span class="hljs-number">12</span>小时的时间<br>%R 显示小时和分钟：hh:mm<br>%S 十进制的秒数<br>%t 水平制表符<br>%T 显示时分秒：hh:mm:ss<br>%u 每周的第几天，星期一为第一天 （值从<span class="hljs-number">1</span>到<span class="hljs-number">7</span>，星期一为<span class="hljs-number">1</span>）<br>%U 第年的第几周，把星期日作为第一天（值从<span class="hljs-number">0</span>到<span class="hljs-number">53</span>）<br>%V 每年的第几周，使用基于周的年<br>%w 十进制表示的星期几（值从<span class="hljs-number">0</span>到<span class="hljs-number">6</span>，星期天为<span class="hljs-number">0</span>）<br>%W 每年的第几周，把星期一做为第一天（值从<span class="hljs-number">0</span>到<span class="hljs-number">53</span>）<br>%x 标准的日期串<br>%X 标准的时间串<br>%y 不带世纪的十进制年份（值从<span class="hljs-number">0</span>到<span class="hljs-number">99</span>）<br>%Y 带世纪部分的十制年份<br>%z，%Z 时区名称，如果不能得到时区名称则返回空字符。<br></code></pre></td></tr></table></figure>

<p><strong>举例子</strong></p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-built_in">strftime</span>(tmpbuf,<span class="hljs-number">128</span>,<span class="hljs-string">&quot;Todayis%A,day%dof%Bintheyear%Y.\n&quot;</span>,newtime);<br><span class="hljs-comment">//Today is Saturday, day 30 of July in the year 2005.</span><br><br><span class="hljs-built_in">strftime</span>(timE,<span class="hljs-number">80</span>,<span class="hljs-string">&quot;Data:\n%Y-%m-%d\nTime:\n%I:%M:%S\n&quot;</span>,timeinfo);<br><span class="hljs-comment">//Data:</span><br><span class="hljs-comment">//2010-09-02</span><br><span class="hljs-comment">//Time:</span><br><span class="hljs-comment">//04:22:11</span><br><span class="hljs-built_in">strftime</span>(str,<span class="hljs-built_in">sizeof</span>(str),<span class="hljs-string">&quot;Itisnow%I%p&quot;</span>,ptr);<br><span class="hljs-comment">//It is now 4PM</span><br></code></pre></td></tr></table></figure>

<p><strong>具体调用方式</strong></p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;iostream&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;time.h&gt;</span></span><br><span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> std;<br><br><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>&#123;<br>    <span class="hljs-type">time_t</span> timep;<br>    <span class="hljs-built_in">time</span>(&amp;timep);<br>    <span class="hljs-type">char</span> tmp[<span class="hljs-number">256</span>];<br>    <span class="hljs-built_in">strftime</span>(tmp, <span class="hljs-built_in">sizeof</span>(tmp), <span class="hljs-string">&quot;%Y-%m-%d %H:%M:%S&quot;</span>, <span class="hljs-built_in">localtime</span>(&amp;timep));<br>    cout &lt;&lt; tmp &lt;&lt; endl;<br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<br>
<br>
<br>

<blockquote>
<p><strong><em>写在最后:</em></strong><br>我叫风骨散人，名字的意思是我多想可以不低头的自由生活，可现实却不是这样。家境贫寒，总得向这个世界低头，所以我一直在奋斗，想改变我的命运给亲人好的生活，希望同样被生活绑架的你可以通过自己的努力改变现状，深知成年人的世界里没有容易二字。目前是一名在校大学生，预计考研，热爱编程，热爱技术，喜欢分享，知识无界，希望我的分享可以帮到你！<br>如果有什么想看的，可以私信我，如果在能力范围内，我会发布相应的博文！<br>感谢大家的阅读！😘 你的点赞、收藏、关注是对我最大的鼓励！</p>
</blockquote>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/C-C%E8%AF%AD%E8%A8%80/" class="category-chain-item">C++&amp;C语言</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>『C++&amp;C语言』c++如何获取系统当前时间（超级详细）</div>
      <div>http://example.com/2023/12/06/『C++&amp;C语言』c++如何获取系统当前时间（超级详细）/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Chiam</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年12月6日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a class="print-no-link" target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2023/12/06/%E3%80%8EC++&amp;C%E8%AF%AD%E8%A8%80%E3%80%8FC++%E5%BE%AA%E7%8E%AF%E7%BB%93%E6%9E%84/" title="『C++&amp;C语言』C++循环结构">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">『C++&amp;C语言』C++循环结构</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2023/12/06/%E3%80%8EC++&amp;C%E8%AF%AD%E8%A8%80%E3%80%8Fc++%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E8%BF%9B%E9%98%B6-%E5%BC%95%E7%94%A8%E4%B8%8E%E5%B8%B8%E9%87%8F/" title="『C++&amp;C语言』c++从入门到进阶-引用与常量">
                        <span class="hidden-mobile">『C++&amp;C语言』c++从入门到进阶-引用与常量</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments" lazyload>
      
  <div id="valine"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#valine', function() {
      Fluid.utils.createScript('https://lib.baomitu.com/valine/1.5.1/Valine.min.js', function() {
        var options = Object.assign(
          {"appId":"fIfc7WqUDZohlQuPc2lz5mJy-MdYXbMMI","appKey":"zjlAG3ZA3o4cBHVAkjzc2Z20","path":"window.location.pathname","placeholder":"留言仅限讨论，禁止广告等行为","avatar":"retro","meta":["nick","mail","link"],"requiredFields":[],"pageSize":10,"lang":"zh-CN","highlight":false,"recordIP":false,"serverURLs":"https://fifc7wqu.api.lncldglobal.com","emojiCDN":null,"emojiMaps":null,"enableQQ":false},
          {
            el: "#valine",
            path: window.location.pathname
          }
        )
        new Valine(options);
        Fluid.utils.waitElementVisible('#valine .vcontent', () => {
          var imgSelector = '#valine .vcontent img:not(.vemoji)';
          Fluid.plugins.imageCaption(imgSelector);
          Fluid.plugins.fancyBox(imgSelector);
        })
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <meta name="referrer" content="no-referrer" /> <footer id="footer" role="contentinfo"> <div class="divider"> <div class="wall"></div> <img class="animals" src="/img/footer_animals_new.png" srcset="/img/loading.gif" lazyload alt="Footer Animals"> </div> <div class="container" data-index="450"> <p> <a href="https://chiamzhang.github.io" target="_blank">DogEgg</a> <i class="iconfont icon-love"></i> <a href="#" target="_blank">LittePig</a> </p> <p> Powered by  <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-pen"></i> Theme  <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> </p> </div> </footer> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.4/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.20.1/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>




  
<script src="/js/love.js"></script>
<script src="/js/funnyTitle.js"></script>
<script src="/js/backTop.js"></script>
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/xiaoxuehua.js"></script>



<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/wanko.model.json"},"display":{"position":"left","width":150,"height":150,"hOffset":20,"vOffset":0},"mobile":{"show":false,"scale":0.5},"react":{"opacity":0.9},"log":false});</script></body>
</html>
